package com.weipai.springboot.model;


import com.weipai.springboot.util.PropUtils;

import java.util.*;

import static com.weipai.springboot.util.MessageDigestUtils.ciphertextForMD5;

/**
 * @author gyt
 * @date 2017-10-30 10:28
 */
public class WxPay {

    private static final String appId = PropUtils.getString("appId");

    private static final String partnerId = PropUtils.getString("mchId");

    private static final String key = PropUtils.getString("key");

    private String nonceStr=nonceStr();//随机字符串

    private String prepayId;//预付费Id 微信统一支付接口返回的

    private String packAge="Sign=WXPay";

    private String timeStamp=timeStamp();//时间戳

    private String sign;//签名
    

    public String getAppId() {
        return appId;
    }


    public String getPartnerId() {
        return partnerId;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getPrepayId() {
        return prepayId;
    }

    public void setPrepayId(String prepayId) {
        this.prepayId = prepayId;
    }

    public String getPackAge() {
        return packAge;
    }

    public void setPackAge(String packAge) {
        this.packAge = packAge;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    //生成随机字符串
    public String nonceStr(){

        return UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
    }
    //生成时间戳
    public String timeStamp(){
        return String.valueOf(System.currentTimeMillis()/1000);
    }
    //组合签名

    public static String getKey() {
        return key;
    }

    public String Sign(WxPay wxPay){

        SortedMap<String,Object> map = new TreeMap<String, Object>();
        map.put("appid",wxPay.getAppId());
        map.put("prepayid",wxPay.getPrepayId());
        map.put("partnerid",wxPay.getPartnerId());
        map.put("noncestr",wxPay.getNonceStr());
        map.put("package",wxPay.getPackAge());
        map.put("timestamp",wxPay.getTimeStamp());
        map.put("sign",wxPay.getSign());
        Collection<String> keyset= map.keySet();

        List list=new ArrayList<String>(keyset);

        Collections.sort(list);
        SortedMap<Object,Object> map2 = new TreeMap<Object, Object>();
        for(int i=0;i<list.size();i++){
            map2.put(list.get(i),map.get(list.get(i)));
        }


        StringBuffer sb = new StringBuffer();
        Set es = map2.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)&& !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
//        String md5 = ciphertextForMD5(sb.toString()+"key=u89wgxc9j89tfbnm02l0543329cjkhga").toUpperCase();
        String md5 = ciphertextForMD5(sb.toString()+"key=gonglaodagonglaodagonglaoda88999").toUpperCase();
        return md5;

    }
}